﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Kugar.Core.BaseStruct;
using Kugar.Core.Collections;

namespace Kugar.UI.WinForm
{
    public class TotalColumnRowBaseControlDesigner : ControlDesigner
    {
        public TotalColumnRowBaseControlDesigner()
            : base()
        {
            //var v = new CustomControlActionList(this.Component, this);

            //DesignerVerb verb = new DesignerVerb("设置合计列", new EventHandler(OnSetTotalColumns));
            //this.Verbs.Add(v);
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection collection = new DesignerActionListCollection();

                collection.Add(new CustomControlActionList(this.Component, this));
                return collection;
            }
        }

    }

    public class CustomControlActionList : DesignerActionList
    {
        private DesignerActionUIService _service;
        private TotalColumnRowBaseControlDesigner _designer;

        public CustomControlActionList(IComponent component, TotalColumnRowBaseControlDesigner designer)
            : base(component)
        {
            this._designer = designer;
            this._service = component.Site.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
            this.AutoShow = true;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            var item = new DesignerActionItemCollection();
            //item.Add(
            //        new DesignerActionPropertyItem("Text",
            //        "Text String", "Appearance",
            //         "Sets the display text."));
            //return item;


            item.Add(new DesignerActionHeaderItem("Appearance"));
            item.Add(
                    new DesignerActionPropertyItem("GridSource",
                     "源网格控件", "Appearance",
                     "设置关联的网格控件"));

            item.Add(
                     new DesignerActionPropertyItem("Columns",
                                                    "关联列", "Appearance",
                                                    "设置关联的网格控件"));

            return item;
        }

        public DataGridView GridSource
        {
            get
            {
                var s = (Control)this.Component;

                var p = s.GetType().GetProperty("GridSource");

                //return ((TotalColumnRowBase)this.Component).GridSource;

                return (DataGridView)p.GetValue(this.Component, null);
            }
            set
            {
                var s = (Control)this.Component;

                var p = s.GetType().GetProperty("GridSource");

                //PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(TotalColumnRowBase))["GridSource"];
                //descriptor.SetValue(this.Component, value);

                p.SetValue(this.Component,value,null);

                this._service.Refresh(this.Component);
            }
        }

        public Control SourceControl
        {
            get { return (Control) this.Component; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(ColumnsUITypeDesign), typeof(UITypeEditor))]
        public DictionaryEx<DataGridViewColumn, TotalColumnConfig> Columns
        {
            get
            {
                var s = (Control) this.Component;

                var p=s.GetType().GetProperty("Columns");

                return (DictionaryEx<DataGridViewColumn, TotalColumnConfig>)p.GetValue(this.Component, null);
            }
            set
            {

                var p = ((Control)this.Component).GetType().GetProperty("Columns");

                //PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(TotalColumnRowBase))["Columns"];
                //var columns=(DictionaryEx<DataGridViewColumn, TotalColumnConfig>)descriptor.GetValue(this.Component);

                var columns = (DictionaryEx<DataGridViewColumn, TotalColumnConfig>)p.GetValue(this.Component, null);

                columns.Clear();

                foreach (var config in value)
                {
                    columns.Add(config);
                }

                this._service.Refresh(this.Component);
                
            }
        }




        public class ColumnsUITypeDesign : UITypeEditor
        {
            /// <summary>
            /// 获取由 EditValue 方法使用的编辑器样式
            /// </summary>
            /// <param name="context"></param>
            /// <returns></returns>
            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                if (context != null && context.Instance != null)
                {
                    return UITypeEditorEditStyle.Modal;
                }
                return base.GetEditStyle(context);
            }

            /// <summary>
            /// 使用指定的服务提供程序和上下文编辑指定对象的值。
            /// </summary>
            /// <param name="context">可用于获取附加上下文信息的 ITypeDescriptorContext。</param>
            /// <param name="provider">IServiceProvider，通过它可能获得编辑服务。</param>
            /// <param name="value">正在编辑的值的实例。</param>
            /// <returns>新的对象值。如果该对象的值尚未更改，则这应返回与传递给它的对象相同的对象</returns>
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                //为 UITypeEditor 提供一个接口，用于显示 Windows 窗体，或者在设计模式下在属性网格控件的下拉区域中显示控件。
                IWindowsFormsEditorService iws = (IWindowsFormsEditorService)provider.GetService(
                    typeof(IWindowsFormsEditorService));

                if (iws != null)
                {
                    var tempSource = (CustomControlActionList) context.Instance;

                    var targetDG = tempSource.GridSource;

                    if (targetDG == null)
                    {
                        MessageBox.Show("请先设置GridSource属性后,再设置Columns属性");
                        return value;
                    }

                    var valuelist = (DictionaryEx<DataGridViewColumn, TotalColumnConfig>)value;

                    using (var UIControl = new TotalColumnRowColumnConfigForm(valuelist, targetDG))
                    {
                        if (iws.ShowDialog(UIControl) == DialogResult.OK)
                        {
                            //MessageBox.Show(valuelist.Count.ToString());
                            valuelist.Clear();

                            foreach (var control in UIControl.Value)
                            {
                                valuelist.Add(control.Key, control.Value);
                            }

                            context.OnComponentChanged();
                        }
                    }

                    return valuelist;
                }
                return value;
            }
        }
    }
}
